package com.example.ruihuada.base

import android.content.Context
import android.os.Bundle
import android.view.WindowManager
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.example.ruihuada.R
import com.example.ruihuada.widget.ToolBarBuild
import com.example.util.ToastUtils
import com.gyf.barlibrary.ImmersionBar
import com.noober.background.BackgroundLibrary
import kotlinx.android.synthetic.main.activity_base.*


abstract class BaseActivity : AppCompatActivity() {

    protected var mContext: Context? = null
    protected var mImmersionBar: ImmersionBar? = null
    private var emptyWarpLayout: FrameLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        BackgroundLibrary.inject(this)
        super.onCreate(savedInstanceState)
        mContext = this
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        if (isActionBar()) {
            setContentView(R.layout.activity_base)
            findViewById<FrameLayout>(R.id.fl_content).addView(
                layoutInflater.inflate(
                    getLayoutId(),
                    null
                )
            )
        } else {
            setContentView(R.layout.activity_base_without_toolbar)
            findViewById<FrameLayout>(R.id.fl_content).addView(
                layoutInflater.inflate(
                    getLayoutId(),
                    null
                )
            )
        }
        emptyWarpLayout = findViewById(R.id.emptyWarpLayout)
        initImmersionBar()
        initView()
        startLiveBusObserve()

    }

    fun buildToolBar(block: ToolBarBuild.() -> Unit): ToolBarBuild {
        val toolBarBuild: ToolBarBuild = ToolBarBuild(this).apply(block)
        return toolBarBuild.build(toolBarLayout)
    }

    abstract fun getLayoutId(): Int
    abstract fun initView()
    abstract fun isActionBar(): Boolean


    protected open fun initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this)
        mImmersionBar?.statusBarDarkFont(true)
        mImmersionBar?.statusBarColor(R.color.colorWhite)
        mImmersionBar?.navigationBarColor(R.color.colorWhite)
        mImmersionBar?.navigationBarDarkIcon(true)
        mImmersionBar?.init()
    }


    override fun onDestroy() {
        super.onDestroy()
        mImmersionBar?.destroy()
    }


    fun showToast(content: String) {
        ToastUtils.show(this, content)
    }


    protected open fun startLiveBusObserve() {}

}